<?php

class PluginController extends Zend_Controller_Action
{
    function init()
    {
        $this->db = Zend_Registry::get("db");
    }
    
    function preDispatch()
    {
		$this->_helper->layout->disableLayout();
    }
	
	function getInfoAction()
	{
		$data_json = file_get_contents("http://www.haodanku.com/?json=true");
		
		$data_array = Zend_Json::decode($data_json);
		
		$mod_ext = new Sys_Extensions();
		
		$mod_ext->FormatArray($data_array);
		
		die;
	}
	
	function testAction()
	{
		$mod_ext = new Sys_Extensions();
		
		$mod = new Db_Tables_LogsQqItems();
		$row = $mod->fetchRow("id > 0", "id DESC");
		
		if($row)
		{
			$data = explode("\n", trim($row['contents']));
			$ext = new Sys_Extensions();
			$ext->FormatArray($data);
			
			echo "<br />==============================================<br />";
			
			$ct = count($data);
			
			if(6 == $ct)
			{
				$u1 = $data['3'];
				$u2 = $data['4'];
			}else{
				$u1 = $data['4'];
				$u2 = $data['5'];
			}
			
			if($u1)
			{
				$d2 = explode("：", $u1);
				if(2 > count($d2))
				{
					$d2 = explode(":", $u1);
				}
				if(2 != count($d2))
				{
					unset($d2[0]);
					$url1 = implode(":", $d2);
				}else{
					$url1 = $d2[1];
				}
				$url1 = str_replace("&amp;", "&", trim($url1));
				echo "Coupon Url: ".$url1."<br>";
			}

			if($u2)
			{
				$d3 = explode("：", $u2);
				if(2 > count($d3))
				{
					$d3 = explode(":", $u2);
				}
				if(2 != count($d3))
				{
					unset($d3[0]);
					$url2 = implode(":", $d3);
				}else{
					$url2 = $d3[1];
				}
				$url2 = str_replace("&amp;", "&", trim($url2));
				echo "Product Url: ".$url2;
			}
			
			echo "<br />==============================================<br />";
			
			$d4_json = file_get_contents("http://pub.alimama.com/items/search.json?q=".urlencode($url2));
			$d4 = Zend_Json::decode($d4_json);
			
			if($d4)
			{
				$mod_ext->FormatArray($d4['data']['pageList']);
			}
			
			echo "<br />==============================================<br />";
			
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url1);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4');
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$data = curl_exec($ch);
			curl_close($ch);
			
			if(trim($data))
			{
				$regex1="/<div class=\"coupon\-info\".*?>.*?<\/div>/ism"; 
				preg_match($regex1, $data, $matches);
				if($matches[0])
				{
					//coupon value
					$regex2="/<dt>(.*?)元优惠券<\/dt>/ism"; 
					preg_match($regex2, $data, $matches2);
					$coupon_value = trim($matches2[1]);
					echo "coupon value = ".$coupon_value."<br />";
					
					//coupon remaining
					$regex3="/<dd>剩<span class\=\"rest\">(.*?)<\/span>/ism"; 
					preg_match($regex3, $data, $matches3);
					$coupon_remaining = trim($matches3[1]);
					echo "coupon remaining = ".$coupon_remaining."<br />";
					
					//coupon used
					$regex4="/已领用<span class\=\"count\">(.*?)<\/span>/ism"; 
					preg_match($regex4, $data, $matches4);
					$coupon_used = trim($matches4[1]);
					echo "coupon used = ".$coupon_used."<br />";
					
					//min order amount
					$regex5="/<dd>单笔满(.*?)元可用/ism"; 
					preg_match($regex5, $data, $matches5);
					$min_order_amount = trim($matches5[1]);
					echo "min order amount = ".$min_order_amount."<br />";
					
					//max coupon qty
					$regex6="/每人限领(.*?)张/ism"; 
					preg_match($regex6, $data, $matches6);
					$max_coupon_qty = trim($matches6[1]);
					echo "max coupon qty = ".$max_coupon_qty."<br />";
					
					//date start
					$regex7="/有效期:(.*?)至/ism"; 
					preg_match($regex7, $data, $matches7);
					$date_start = trim($matches7[1]);
					echo "date start = ".$date_start."<br />";
					
					//date expired
					$regex8="/至(.*?)<\/dd>/ism"; 
					preg_match($regex8, $data, $matches8);
					$date_expired = trim($matches8[1]);
					echo "date expired = ".$date_expired."<br />";
				}
			}
		}
		die;
	}
	
	function t1Action()
	{
		$data = array(
			'member_id' => 666,
			'member_token' => sha1("l2kl3hfkshfds"),
			'data' => array(
				"a1" => array(
					100 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					),
					101 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					),
					102 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					)
				),
				"a2" => array(
					100 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					),
					101 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					),
					102 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					)
				),
				"a3" => array(
					100 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					),
					101 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					),
					102 => array(
						"taoToken" => "￥klmYRhifih￥",
						"qrCodeUrl" => "http://gqrcode.alicdn.com/img?type=hv&text=https://s.click.taobao.com/CfUJc8x&h=300&w=300",
						"shortLinkUrl" => "https://s.click.taobao.com/9XaJc8x"
					)
				)
			)
		);
		
		$js = Zend_Json::encode($data);
		
		echo $js;
		die;
	}
	
	function cleanTestDataAction()
	{
		$this->db->query("truncate table `bjm_members_link`");
		
		echo "OK";
		die;
	}
	
	function test2Action()
	{
		$uri= "http://bjm/account/port-receive-links";
		
		$data = array(
			1 => array(
				'linkid' => 1722,
				'linktype' => 1,
				'shortLinkUrl' => 'a',
				'qrCodeUrl' => 'a',
				'taoToken' => 'a'
			),
			2 => array(
				'linkid' => 2061,
				'linktype' => 1,
				'shortLinkUrl' => 'b',
				'qrCodeUrl' => 'b',
				'taoToken' => 'b'
			)
		);
		
		$data_str = Zend_Json::encode($data);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $uri);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);
		
		if (curl_errno($ch)) {
			echo 'Errno'.curl_error($ch);
		}
		curl_close($ch);

		$result = Zend_Json::decode($tmpInfo);
		var_dump($result);
		die;
	}
	
	function viewLinksAction()
	{
		$mod_links = new Db_Tables_BjmMembersLink();
		$rows = $mod_links->fetchAll();
		$rows = $rows->toArray();
		
		$ext = new Sys_Extensions();
		$ext->FormatArray($rows);
		
		die;
	}
	
	function test3Action()
	{
		$data = "data%5B0%5D%5Bid%5D=193&data%5B0%5D%5Blinkid%5D=817&data%5B0%5D%5Blinktype%5D=1&data%5B0%5D%5Blinkurl%5D=https%253A%252F%252Fdetail.tmall.com%252Fitem.htm%253Fid%253D524565968643&data%5B0%5D%5BtaoToken%5D=%EF%BF%A5PWEDjtDpYC%EF%BF%A5&data%5B0%5D%5BqrCodeUrl%5D=http%3A%2F%2Fgqrcode.alicdn.com%2Fimg%3Ftype%3Dhv%26text%3Dhttps%3A%2F%2Fs.click.taobao.com%2Fgriel5x%26h%3D300%26w%3D300&data%5B0%5D%5BshortLinkUrl%5D=https%3A%2F%2Fs.click.taobao.com%2FeNjel5x&data%5B1%5D%5Bid%5D=194&data%5B1%5D%5Blinkid%5D=817&data%5B1%5D%5Blinktype%5D=2&data%5B1%5D%5Blinkurl%5D=https%253A%252F%252Fdetail.tmall.com%252Fitem.htm%253Fid%253D524565968643&data%5B1%5D%5BtaoToken%5D=%EF%BF%A51qTqjtDuXf%EF%BF%A5&data%5B1%5D%5BqrCodeUrl%5D=http%3A%2F%2Fgqrcode.alicdn.com%2Fimg%3Ftype%3Dhv%26text%3Dhttps%3A%2F%2Fs.click.taobao.com%2Fgriel5x%26h%3D300%26w%3D300&data%5B1%5D%5BshortLinkUrl%5D=https%3A%2F%2Fs.click.taobao.com%2FeNjel5x";
		
		$data = urldecode($data);
		
		var_dump($data);
		die;
	}
	
	function test4Action()
	{
		$ext = new Sys_Extensions();
		
		$url = "https://s.click.taobao.com/7xnqL3x";
		$long_url = $ext->get_redirect_url($url);
		$tu_url = $ext->get_redirect_url($long_url);
		
		$ex = explode("=", $tu_url);
		$link = urldecode($ex[1]);
		
		
		var_dump($ext->get_redirect_url($link, $tu_url));
		die;
	}
	
	function autoUpdateProductsAction()
	{
		ini_set('max_execution_time', '900');
		
		/**
		 * sale_in_30_days
		 * coupon_remaining_qty
		 * coupon_used_qty
		 */
		
		$mod_products = new Db_Tables_BjmProducts();
		$products = $mod_products->GetActiveProductsToRefresh();
		
		if(!empty($products))
		{
			$n = 1;
			while($n < 10000)
			{
				foreach($products as $pid => $data)
				{
					$set = array("last_refresh" => date("Y-m-d H:i:s"));
					$adapater = $mod_products->getAdapter();
					$where = $adapater->quoteInto("product_id = ?", $pid);
					$mod_products->update($set, $where);
				}
				
				$n += 1;
			}
		}
		
		echo $n;die;
	}
	
	function testTkAction()
	{
									$d4_json = file_get_contents("http://pub.alimama.com/items/search.json?q=".urlencode("https://detail.tmall.com/item.htm?id=545095219719"));
									$d4 = Zend_Json::decode($d4_json);

									if(is_array($d4))
									{
										if(1 == count($d4['data']['pageList']) && isset($d4['data']['pageList'][0]))
										{
											$pinfo = $d4['data']['pageList'][0];
											
											if(isset($pinfo['tkSpecialCampaignIdRateMap']))
											{
												if(count($pinfo['tkSpecialCampaignIdRateMap']))
												{
													$tkSpecialCampaignIdRateMap = $pinfo['tkSpecialCampaignIdRateMap'];
													arsort($tkSpecialCampaignIdRateMap);
													foreach($tkSpecialCampaignIdRateMap as $tkey => $tval)
													{
														if(0 < $tval)
														{
															$campaignId = $tkey;
														}
														break;
													}
												}
											}
										}
										
										if($pinfo['sellerId'] && $d4['info']['pvid'] && $campaignId)
										{
											$ex5 = explode("_", $d4['info']['pvid']);
											
											if(count($ex5))
											{
												rsort($ex5);
												echo "http://pub.alimama.com/shopdetail/campaigns.json?oriMemberId=".$pinfo['sellerId']."&t=".$ex5[0]."&pvid=".$d4['info']['pvid'];die;
												if($ex5[0])
												{
													$d5_json = file_get_contents("http://pub.alimama.com/shopdetail/campaigns.json?oriMemberId=".$pinfo['sellerId']."&t=".$ex5[0]."&pvid=".$d4['info']['pvid']);
													$d5 = Zend_Json::decode($d5_json);
													var_dump($d5);die;
													if(isset($d5['data']['campaignList'][0]['shopKeeperId']))
													{
														echo "http://pub.alimama.com/myunion.htm?#!/promo/self/campaign?campaignId=".$campaignId."&shopkeeperId=".$d5['data']['campaignList'][0]['shopKeeperId']."&userNumberId=".$pinfo['sellerId']."&tab=2";
													}
												}
											}
										}
									}
									
									echo "<br />End.";
									die;
	}
	
	function test6Action()
	{
		
	}
	
	function test7Action()
	{
		$data = array(
			102 => array(
				'shortLinkUrl' => "https://s.click.taobao.com/pAdm91x",
				"taoToken" => "￥RmF7PLVKm2￥"
			),
			105 => array(
				'shortLinkUrl' => "https://s.click.taobao.com/lxLk91x",
				"taoToken" => "￥LkttPLfrSn￥"
			),
			110 => array(
				'shortLinkUrl' => "https://s.click.taobao.com/6Dbj91x",
				"taoToken" => "￥XWgnPLU3mL￥"
			)
		);
		
		echo Zend_Json::encode($data);
		die;
	}
	
	function getStrAction()
	{
		$hallo = '[CQ:image,file=1AB5F8D7831E613A10374A1C5CE2823A.jpg]
火爱  潮流金头毛毛低跟平底单鞋
原价59.9元【券后29.9元】包邮
领券：https://shop.m.taobao.com/shop/coupon.htm?activityId=f3a6a57f962b4888894e892cd75be05f&sellerId=894017634
抢购：https://s.click.taobao.com/4LA3zww
【天猫正品保障】欧美风格 舒适柔软，透气轻便， 潮流尖头 青春靓丽~';
		
//		preg_match("/(https:\/\/s\.|https:\/\/(detail|world)\.)(?:[\w]+\.)([a-zA-Z\.]{2,6})([\/\w\.-\?\&]*)*\/?/", $hallo, $subject);
//		print_r($subject);
//		
//$str = preg_replace("/(https:\/\/s\.|https:\/\/detail\.)(?:[\w]+\.)([a-zA-Z\.]{2,6})([\/\w\.-\?\&]*)*\/?/", "a website", $hallo);
//print $str;

		$ext = new Sys_Extensions();
		$a = $ext->GetProductUrl($hallo);
		$ext->FormatArray($a);
		
		die;
	}
	
	function test9Action()
	{
		$mod_pending_msg = new Db_Tables_BjmBotsPendingMsg();
		$mod_pending_msg->from_id = 1;
		$mod_pending_msg->member_id = 1;
		$mod_pending_msg->pid = "mm_119182188_20914258_70730675";
		$mod_pending_msg->contents = '[CQ:image,file=1AB5F8D7831E613A10374A1C5CE2823A.jpg]
火爱  潮流金头毛毛低跟平底单鞋
原价59.9元【券后29.9元】包邮
领券：https://shop.m.taobao.com/shop/coupon.htm?activityId=f3a6a57f962b4888894e892cd75be05f&sellerId=894017634
抢购：[[]]
【天猫正品保障】欧美风格 舒适柔软，透气轻便， 潮流尖头 青春靓丽~';
		$mod_pending_msg->url = "https://detail.tmall.com/item.htm?id=544455934983";
		$mod_pending_msg->AddAction();
		die;
	}
	
	function test10Action()
	{
		$available_from_qq_groups = array(
			array("from_id" => 1),
			array("from_id" => 2)
		);
		
		$mod = new Db_Tables_BjmBotsTo();
		$a = $mod->GetToInfoByFromGroupInfo($available_from_qq_groups);
		var_dump($a);
		die;
	}
}